﻿using Assets.Scripts.DL.Enemy.CommonEnemy;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using static System.TimeZoneInfo;

namespace Assets.Scripts.DL.Enemy.SpriderBoss {
	public class SpiderBossCure : CommonState<SpiderBossController> {
		Timer _cureTimer = new();
		//bool _isCured = false;//只能治疗一次
		bool _cure = false;
		public SpiderBossCure(SpiderBossController controller, StateEnum stateName) : base(controller, stateName) {
			AddConditionToSelf(() => _controller.StateData.CurHP < _controller.Config.MaxHP / 2 && !_controller.IsCured);
		}

		public override void Enter() {
			_controller.Velocity = Vector2.zero;
			_controller.PlayAnim(StateEnum.Atk4);
			_cureTimer.Reset();
			_controller.StartCoroutine(TransitionColor(0.3f, _controller.sprite.color, Color.green));
			//_controller.StartCoroutine(AddHp());
			_cure = false;
		}

		public override void Exit() {
			_controller.IsCured = true;
			_controller.ShoudCure = false;
			_controller.sprite.color = Color.white;

			_controller.StartCoroutine(TransitionColor(0.3f, _controller.sprite.color, Color.white));
		}

		public override void Update() {
			if (_controller.IsAnimPlayAt(_controller.BarrageNormalTime) && !_cure) {
				_controller.StateData.CurHP += _controller.CureHp;
				//Debug.Log(_controller.StateData.CurHP);
				_cure = true;
#warning TODO 加血特效
			}
		}

		IEnumerator TransitionColor(float sec, Color srcColor, Color destColor) {
			Timer _timer = new();

			while (_timer.Elapsed < sec) {
				float t = _timer.Elapsed / sec;
				_controller.sprite.color = Color.Lerp(srcColor, srcColor, t);
				yield return null;
			}
		}
	}
}
